<?php 
/**
 * 退货单款项
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */
class ClientReturnSalePublicModel extends AbsFundsPublicModel {
	//款项类型
	public $object_type				=	123;//退货单款项
	public $sale_object_type		=	120;//退换货单
	//对象类型
	public $comp_type				=	1;
	//对象款项表
	public $object_type_table		=	'client_paid_detail';	
   
	/**
	 * 退货产生的款项
	 *
	 * @param array $id 退货单ID
	 * @return array $vo
	 */
	public function _fund($id){ 
		$id	=	is_array($id)?$id['id']:$id;   
	 	if($id>0) {
			$model	= M('ReturnSaleOrderDetail');
		 	$info	= $model
		 			->field( 	'b.basic_id as basic_id,b.currency_id as currency_id,date(b.return_order_date) as paid_date,
								b.client_id as comp_id,'.$this->comp_type.' as comp_type,b.id as object_id,b.return_sale_order_no as reserve_id,
								sum(quantity*capability*dozen*discount*price) as money,
								0 as account_money,
								c.paid_id as id,
								'.$this->object_type.' as object_type
								')
							->join('as a 
									left join return_sale_order as b on a.return_sale_order_id=b.id and b.id='.$id.' 
									left join '.$this->object_type_table.' as c 
										on c.object_id=a.return_sale_order_id and c.object_type='.$this->object_type.'')
							->where('a.return_sale_order_id='.$id)
							->group('a.return_sale_order_id')
							->find();     
			$comp_name			= SOnly('client',$info['comp_id']);
			$info['comp_name']	= $comp_name['client_name'];     
			$info['id']			= $this->_saveFunds($info);   
			return $info;
		}
	} 
	
	/**
	 * 删除退货单
	 *
	 * @param int or array $id
	 * @return unknown
	 */
	public function deleteOp($id){  
		$id	=	is_array($id)?$id['id']:$id;      
		$return_info	=	M('returnSaleOrderDel')->where('id='.$id)->find();  
		//删除退货单欠款
		if($id>0) {
			$paid_id = $this
			->where('object_id='.$id.' and comp_type='.$this->comp_type.' and object_type='.$this->object_type.' and to_hide=1')
			->getField('id');  
			//款项表ID 
			if ($paid_id>0){
				$vo	=	$this->_deleteFunds($paid_id);  
			}
		}  
		//退+换(删除换货款项)
		if ($return_info['return_sale_order_type']==2 && $return_info['sale_order_id']>0) { 
			$sale_id = $return_info['sale_order_id']; 
			Funds($sale_id,120,'delete');
		}  
		return $vo;
	} 
	
	/**
	 * 插入前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkInsert($info){    
		return	$this->checkClientDate($info);  
	} 
	
	/**
	 * 修改插入数据前的业务规则验证
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkUpdate($info){ 
		return	$this->checkClientDate($info);  
	} 
	
	/**
	 * 验证某日期以前的款项是否可以被插入
	 *
	 * @param array $info
	 * @return array
	 */
	public function checkClientDate($info){ 
		$error['state']	=	1;    
		if ($this->validCheckAccountDate(formatDate($info['return_order_date'],'vf_date'),$info)===false){ 
			$error['state']	=	-1;
			$error['error_msg'][]	=	$info['return_order_date'].','.L('order_date_chk');//已指定操作,不可删除
		}   
		return $error;
	}
 
	/**
	 * 验证是否可修改
	 *
	 * @param array $info
	 * @return bool
	 */
	public function checkEdit($info){ 
		return $this->checkReturn($info); 
	}
	 
	/**
	 * 验证是否可删除
	 *
	 * @param array $info
	 * @return bool
	 */
	public function checkDelete($info){  
		return $this->checkReturn($info);  
	} 
	
	/**
	 * 验证退货的删除与修改条件
	 *
	 * @param array $info
	 * @return bool
	 */
	private function checkReturn($info){
		$error['state']		=	1;  
		//退货ID
		$id	=	is_array($info)?$info['id']:$info; 
		if ($id>0) {
			//款项信息
			$model		=	M($this->getCompPaidDetail($this->comp_type));
			$paid_info	=	$model->where('object_type='.$this->object_type.' and object_id='.$id)->find();  
			//验证是否被指定支付 
			$error		=	parent::checkDelete($paid_info); 
			//验证是否被总平 
			if ($error['state']!=-1) {   
				$error	=	$this->checkAccountDate($paid_info);	 	
			}
		}else{
			$error['state']			=	-1; 
			$error['error_msg'][]	=	L('error_funds_values_error');//款项验证,参数错误,请重新操作
		} 
		return $error; 
	}
	
}
?>